//
//  IndicatorViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class IndicatorViewController: BaseTableViewController {

    let disposeBag = DisposeBag()
    
    lazy var indicator1: UIActivityIndicatorView = {
        let _indicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        self.tableHeadView.addSubview(_indicator)
        _indicator.snp.makeConstraints({ (make) in
            make.center.equalTo(self.tableHeadView)
        })
        return _indicator
    }()
    
    lazy var switchView: UISwitch = {
        let _switchV = UISwitch()
        self.tableHeadView.addSubview(_switchV)
        _switchV.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.bottom.equalTo(indicator1.snp.top).offset(-20)
        })
        return _switchV;
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        indicator1.hidesWhenStopped = false
        setupData()
        _rxSwiftProperty()
    }
    
    private func _rxSwiftProperty() {
        Observable<Int>.timer(3, scheduler: MainScheduler.instance).subscribe {
            [weak self] in
            self?.indicator1.color = UIColor.blue
            print(info: "------")
        }.disposed(by: disposeBag)
        
        // map { $0 }
        switchView.rx.isOn.asObservable().bind(to: indicator1.rx.isAnimating).disposed(by: disposeBag)
        
        /** RxSwift 对 UIApplication 增加了一个名为 isNetworkActivityIndicatorVisible 绑定属性，我们通过它可以设置是否显示联网指示器（网络请求指示器） */
        switchView.rx.value.asObservable().bind(to: UIApplication.shared.rx.isNetworkActivityIndicatorVisible).disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["activityIndicatorViewStyle",
                      "Color",
                      "进度条开始转动",
                      "进度条停止转动",
                      "是否在转动",
                      "hidesWhenStopped"]
        
        let descArr = ["""
                        设置指示器的样式
                        UIActivityIndicatorViewStyleWhiteLarge  比较大的白色环形进度条
                        UIActivityIndicatorViewStyleWhite   白色环形进度条
                        UIActivityIndicatorViewStyleGray    灰色环形进度条
                       """,
                       "设置环形进度条的颜色",
                       "activityIndicator.startAnimating()",
                       "activityIndicator.stopAnimating()",
                       "activityIndicator.isAnimating",
                       "当停止动画的时候，是否隐藏。默认为YES"]
        
        addTitles(titles, footDescs: descArr)
    }

}
